package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.entity.xiangxian.ExamplePoint;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * @author hongjianbin
 * @date 2020/7/20
 */
@RequestMapping("/action")
@RestController
public class ActionController extends BaseController {

    /**
     * 上报活动
     * @param time
     * @param action_address
     * @param join_count
     * @param actionTypeId
     * @param action_content
     * @param action_picture
     * @param userId
     * @return
     * @throws ParseException
     */
    @PostMapping("/addAction")
    public Return addExamplepoint(String name, String time, String action_address, Integer join_count
            , Integer actionTypeId, String action_content, String action_picture, String userId) throws ParseException {

        if (action_content.trim().length() > 500) {
            return Return.getErrorResult("活动内容过长");
        }
        if (actionTypeId==null) {
            return Return.getErrorResult("活动类型名不能为空");
        }
        if (action_address.trim().length() > 100) {
            return Return.getErrorResult("地点输入过长");
        }
        if (name != null) {
            long L = baseInfoService.findActionByName(name, 3);
            if (L > 0) {
                return Return.getErrorResult("活动名已存在!");
            }

        }
        ActionCenter center = new ActionCenter();
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(3, 3);
        center.setAid(String.valueOf(snowFlake.nextId()));
        center.setAdd_userid(userId);
        center.setName(name);
        center.setAction_picture(action_picture);
        center.setAction_content(action_content);
        center.setJoin_count(join_count);
        center.setAction_address(action_address);
        center.setActionTypeId(actionTypeId);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (time != null) {
            center.setTime(DateSwitcher.string2Date(time));
        }

        long l = baseInfoService.addAction(center);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 活动详情
     *
     * @param aid
     * @return
     */
    @PostMapping("/actionDetail")
    public Return getdetail(String aid, String userId) {
        if (aid == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        ActionCenter actionCenter = baseInfoService.actionDetail(aid,userId);
        return Return.getSuccessResult(actionCenter);
    }

    /**
     * 活动列表
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/actionList")
    public Return actionList(Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<ActionCenter> list = baseInfoService.actionList(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }
}
